package com.jiantong.web.open.app.work;

import com.google.common.collect.Maps;
import com.jiantong.work.enums.HarmlessFormsType;
import com.jiantong.work.service.HarmlessReadService;
import com.jiantong.work.service.QuarantineCenterHarmlessReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by niufei on 2017/4/18.
 * 无害化占比图
 */
@Slf4j
@OpenBean
public class HarmlessFormsApis {

    private final static SimpleDateFormat DFT = new SimpleDateFormat("yy-MM-dd");

    @RpcConsumer
    private HarmlessReadService harmlessReadService;

    @RpcConsumer
    private QuarantineCenterHarmlessReadService quarantineCenterHarmlessReadService;


    @OpenMethod(
            key = "find.sum.harmless.forms",
            paramNames = {"fromDate","toDate","animalType"},
            httpMethods = RequestMethod.GET
    )
    public Map<Integer , Object> HarmlessForms(String fromDate , String toDate , Integer animalType){
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Map<Integer , Object> map = Maps.newHashMap();
        Response<List<Map<String , Integer>>> isLocalResponse = harmlessReadService.findSumIsLocal(start,end,animalType);
        if(!isLocalResponse.isSuccess()){
            log.error("failed.sum.isLocal",isLocalResponse.getError());
            throw new OPServerException(isLocalResponse.getError());
        }
        Response<List<Map<String , Integer>>> sumReportType = harmlessReadService.findSumReportType(start,end,animalType);
        if(!sumReportType.isSuccess()){
            log.error("failed.sum.ReportType",sumReportType.getError());
            throw new OPServerException(sumReportType.getError());
        }
        Response<Map<String , Integer>> qResponse = quarantineCenterHarmlessReadService.findSumByProductType(start,end,animalType);
        if(!qResponse.isSuccess()){
            log.error("failed.sum.quarantine",qResponse.getError());
            throw new OPServerException(qResponse.getError());
        }
        Map<String , Integer> map1 = Maps.newHashMap();//产地检疫
        Map<String , Integer> map2 = Maps.newHashMap();//外市入场
        Map<String , Integer> map3 = Maps.newHashMap();//本市入场
        Map<String , Integer> map4 = Maps.newHashMap();//宰前检查
        Map<String , Integer> map5 = Maps.newHashMap();//同步检疫
        Map<String , Integer> map6 = Maps.newHashMap();//防疫中心
        if(qResponse.getResult() != null){
            map6 = qResponse.getResult();
        }

        for(Map<String , Integer> temp : isLocalResponse.getResult()){
            if(temp.get("isLocal") == 1){
                map3.put("quantity",temp.get("quantity"));
            }else {
                map2.put("quantity",temp.get("quantity"));
            }
        }
        for(Map<String , Integer> temp : sumReportType.getResult()){
            switch (temp.get("reportType")){
                case 1:
                    map1.put("quantity",temp.get("quantity"));
                    break;
                case 5:
                    map4.put("quantity",temp.get("quantity"));
                    break;
                case 6:
                    map5.put("quantity",temp.get("quantity"));
                    break;
                default:
                    break;
            }
        }
        map.put(HarmlessFormsType.QUARANTINE_ORIGIN_AREA.value(),map1);
        map.put(HarmlessFormsType.ADMISSION_INSPECTION_NON_LOCAL.value(),map2);
        map.put(HarmlessFormsType.ADMISSION_INSPECTION_LOCAL.value(),map3);
        map.put(HarmlessFormsType.ANTEMORTEM_INSPECTION.value(),map4);
        map.put(HarmlessFormsType.SYNCHRONOUS_INSPECTION.value(),map5);
        map.put(HarmlessFormsType.QUARANTINE_CENTER_HARMLESS.value(),map6);
        return map;
    }

    @OpenMethod(
            key = "find.a.week.harmless.forms",
            paramNames = {"animalType"},
            httpMethods = RequestMethod.GET
    )
    public Map<Integer ,List> findAWeekHarmlessForms(Integer animalType) throws ParseException {
        Map<Integer ,List> result = Maps.newHashMap();
        Response<List<Map<String , Object>>> isLocalResponse = harmlessReadService.findAWeekSumIsLocal(animalType);
        if(!isLocalResponse.isSuccess()){
            log.error("failed.a.week.sum.isLocal",isLocalResponse.getError());
            throw new OPServerException(isLocalResponse.getError());
        }
        Response<List<Map<String , Object>>> sumReportType = harmlessReadService.findAWeekSumReportType(animalType);
        if(!sumReportType.isSuccess()){
            log.error("failed.a.week.sum.ReportType",sumReportType.getError());
            throw new OPServerException(sumReportType.getError());
        }
        Response<List<Map<String , Object>>> qResponse = quarantineCenterHarmlessReadService.findAWeekSumByProductType(animalType);
        if(!qResponse.isSuccess()){
            log.error("failed.a.week.sum.quarantine",qResponse.getError());
            throw new OPServerException(qResponse.getError());
        }
        List<Map<String , Object>> maps1 = new ArrayList<>();//产地检疫
        List<Map<String , Object>> maps2 = new ArrayList<>();//外市入场
        List<Map<String , Object>> maps3 = new ArrayList<>();//本市入场
        List<Map<String , Object>> maps4 = new ArrayList<>();//宰前检查
        List<Map<String , Object>> maps5 = new ArrayList<>();//同步检疫
        List<Map<String , Object>> maps6 = new ArrayList<>();//防疫中心
        for(Map<String , Object> map :isLocalResponse.getResult()){
            if((int)map.get("isLocal") == -1){
                maps2.add(map);
            }else {
                maps3.add(map);
            }
        }
        for(Map<String , Object> map :sumReportType.getResult()){
            switch ((int)map.get("reportType")){
                case 1:
                    maps1.add(map);
                    break;
                case 5:
                    maps4.add(map);
                    break;
                case 6:
                    maps5.add(map);
                    break;
                default:
                    break;
            }
        }
        maps1 = make(maps1);
        maps2 = make(maps2);
        maps3 = make(maps3);
        maps4 = make(maps4);
        maps5 = make(maps5);
        maps6 = make(qResponse.getResult());
        result.put(HarmlessFormsType.QUARANTINE_ORIGIN_AREA.value(),maps1);
        result.put(HarmlessFormsType.ADMISSION_INSPECTION_NON_LOCAL.value(),maps2);
        result.put(HarmlessFormsType.ADMISSION_INSPECTION_LOCAL.value(),maps3);
        result.put(HarmlessFormsType.ANTEMORTEM_INSPECTION.value(),maps4);
        result.put(HarmlessFormsType.SYNCHRONOUS_INSPECTION.value(),maps5);
        result.put(HarmlessFormsType.QUARANTINE_CENTER_HARMLESS.value(),maps6);

        return result;
    }

    private List<Map<String , Object>> make(List<Map<String , Object>> list) throws ParseException {
        List<Map<String , Object>> mapList = new ArrayList<>();
        Calendar[] calendars = new Calendar[7];
        Map<String , Object> temp = null;

        for (int i = 0 ; i<7;i++){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MILLISECOND,0);
            calendar.set(Calendar.DATE ,calendar.get(Calendar.DATE) - i);
            calendars[i] = calendar;
            temp = Maps.newHashMap();
            temp.put("date",DFT.format(calendar.getTime()));
            temp.put("quantity",0);
            mapList.add(temp);
        }
        if(list == null || list.size() == 0){
            return mapList;
        }else{
            for(Map<String , Object> map : list){
                Calendar calendarTemp = Calendar.getInstance();
                Date date = DFT.parse(map.get("date").toString());
                calendarTemp.setTime(date);
                for(int i = 0 ; i < 7 ; i ++){
                    if(calendarTemp.equals(calendars[i])){
                        Map<String ,Object> mapTemp = Maps.newHashMap();
                        mapTemp.put("date",map.get("date"));
                        mapTemp.put("quantity",map.get("quantity"));
                        mapList.set(i,mapTemp);
                    }
                }
            }
            return mapList;
        }
    }
}
